package com.example.designpatterns.command.concrete;

import com.example.designpatterns.command.Command;
import com.example.designpatterns.command.receiver.Door;

/**
 * DoorOpenCommand类 - 门打开命令
 */
public class DoorOpenCommand implements Command {
    private Door door;
    private boolean wasLocked;
    
    /**
     * 创建门打开命令
     * @param door 门对象
     */
    public DoorOpenCommand(Door door) {
        this.door = door;
    }
    
    @Override
    public void execute() {
        // 保存当前状态以便撤销
        wasLocked = door.isLocked();
        
        // 如果门已锁定，先解锁
        if (door.isLocked()) {
            door.unlock();
        }
        
        door.open();
    }
    
    @Override
    public boolean undo() {
        door.close();
        
        // 如果之前是锁定状态，恢复锁定
        if (wasLocked) {
            door.lock();
        }
        
        return true;
    }
    
    @Override
    public String getName() {
        return "打开" + door.getLocation() + "的门";
    }
} 